package com.example.kotlinExample.controller;

import com.alibaba.excel.EasyExcel;
import com.example.kotlinExample.entity.DemoData;
import com.example.kotlinExample.entity.User;
import com.example.kotlinExample.service.UserService;
import com.example.kotlinExample.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author liliang
 */
@RequestMapping("/test")
@RestController
public class TestController {

    @Autowired
    private UserService userService;

    @PostMapping("/import")
    public void importExcel(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), DemoData.class, new ExcelListener()).sheet().doRead();
    }

    @GetMapping("/test")
    public String test() {
        return "sdfsdf";
    }

    @PostMapping("/user/save")
    public User save(@RequestBody User user) {
        return userService.save(user);
    }

    @GetMapping("/es/users")
    public Page<User> getEsUsers(String name) {
        return userService.selectUser(name);
    }

    @GetMapping("/user/{id}")
    public User getUser(@PathVariable Integer id) {
        return userService.getUser(id);
    }

    @GetMapping("/users")
    public List<User> getAll() {
        return userService.getUserList();
    }

    @GetMapping("/users/page")
    public Page<User> getPage() {
        return userService.getPage();
    }

    @GetMapping("/search")
    public Page<User> search(String keyword, Integer pageNum, Integer pageSize) {
        return userService.search(keyword, pageNum, pageSize);
    }

    @GetMapping("/user/search")
    public Page<UserVo> searchUserInfo(String keyword, Integer pageNum, Integer pageSize) {
        return userService.searchUserInfo(keyword, pageNum, pageSize);
    }
}
